package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.dto.XeVenPurchSupInfoPageDto;
import com.xebest.api.dto.XeVenRefundDTO;
import com.xebest.api.dto.XeVenSupplierInfoRequestDTO;
import com.xebest.api.dto.XeVenSupplierInfoResponseDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.common.enums.erp.aftersales.VenOrderRefundStatusEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.constant.XeErrorCode;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.model.XeVenOrderRefundEntity;
import com.xebest.web.model.XeVenPurchSupInfoEntity;
import com.xebest.web.service.XeVenOrderReFundService;
import com.xebest.web.service.XeVenPurchSupplierService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenPurchSupInfoView;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 供货商接口
 *
 * @author 李书峰
 */
@RestController
@RequestMapping("restapi/supplier")
public class XeVenSupplierApi extends BaseController {

    private Logger log = LoggerFactory.getLogger(XeVenSupplierApi.class);

    @Autowired
    private XeVenPurchSupplierService XeVenPurchSupplierService;
    @Autowired
    private XeVenOrderReFundService xeVenOrderReFundService;//退款业务

    @Autowired
    private XeBaseUtil xeBaseUtil;

    /**
     * 供货商code查询接口
     *
     * @author 李书峰
     */
    @RequestMapping("/queryCodes")
    public XeVenSupplierInfoResponseDTO querySupplierCodes(@RequestBody XeVenSupplierInfoRequestDTO dto) {
        XeVenSupplierInfoResponseDTO supplier = new XeVenSupplierInfoResponseDTO();
        List<XeVenSupplierInfoResponseDTO> supplierCodes = null;
        try {
            String incomParamJson = JSONObject.toJSONString(dto);
            log.info("供货商查询接口传入参数： {}", incomParamJson);
            //1、验证参数
/*			boolean parError = true;
			if(null != dto 
					&& StringUtils.isNotBlank(dto.getName())){
				parError = false;
			}
			if( parError ){
				return supplierCodes;
			}*/
            //2、拼装参数
            XeVenPurchSupInfoEntity supplierEntity = JSONObject.parseObject(JSONObject.toJSONString(dto), XeVenPurchSupInfoEntity.class);
            if (null == supplierEntity) {
                supplierEntity = new XeVenPurchSupInfoEntity();
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            supplierEntity.setUserCode(userInfo.getUserCode());
            supplierEntity.setPageNum(dto.getPageNum());
            supplierEntity.setPageSize(dto.getPageSize());
//			XePageInfo<XeVenSupplierInfoResponseDTO> xePageInfo;
//			PageHelper.startPage(dto.getPageNum(),dto.getPageSize(), true);
            //3、返回结果
            List<XeVenPurchSupInfoView> vewList = XeVenPurchSupplierService.querySupplierCodes(supplierEntity);
            if (null != vewList && vewList.size() > 0) {
                supplierCodes = new ArrayList<XeVenSupplierInfoResponseDTO>();
                for (XeVenPurchSupInfoView view : vewList) {
                    XeVenSupplierInfoResponseDTO supplierCode = new XeVenSupplierInfoResponseDTO();
                    supplierCode.setSupplierCode(view.getSupplierCode());
                    supplierCode.setSupplierName(view.getSupplierName());
                    supplierCodes.add(supplierCode);
                }
//				xePageInfo = new XePageInfo<XeVenSupplierInfoResponseDTO>(supplierCodes);
            }
        } catch (Exception e) {
            log.error("供货商查询接口失败：{}", e);
            return null;
        }
        supplier.setSupplierList(supplierCodes);
        return supplier;
    }


    /**
     * 查询采购供货商信息
     * 支付中心 > 结算管理 > 结算白名单使用
     *
     * @author 李贝贝
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping("/listPurchSupInfo")
    public XeVenPurchSupInfoPageDto listPurchSupInfo(@RequestBody XeVenSupplierInfoRequestDTO dto) {
        XeVenPurchSupInfoPageDto pageDto = new XeVenPurchSupInfoPageDto();
        // 分页查询
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        try {
            String incomParamJson = JSONObject.toJSONString(dto);
            log.info("支付中心 > 结算管理 > 结算白名单使用接口传入参数： {}", incomParamJson);
            //2、拼装参数
            XeVenPurchSupInfoEntity supplierEntity = new XeVenPurchSupInfoEntity();
            supplierEntity.setSupplierCode(dto.getSupplierCode());//供货商编码
            supplierEntity.setSupplierName(dto.getSupplierName());//供货商名称
            //3、返回结果
            List<XeVenPurchSupInfoView> resultList = XeVenPurchSupplierService.listPurchSupInfo(supplierEntity);
            List<XeVenSupplierInfoResponseDTO> transferList = new ArrayList<XeVenSupplierInfoResponseDTO>();
            for (XeVenPurchSupInfoView view : resultList) {
                XeVenSupplierInfoResponseDTO responseDTO = new XeVenSupplierInfoResponseDTO();
                responseDTO.setSupplierCode(view.getSupplierCode());
                responseDTO.setSupplierName(view.getSupplierName());
                responseDTO.setCreateDate(view.getCreateDate());
                responseDTO.setModifyDate(view.getModifyDate());
                responseDTO.setUserCode(view.getUserCode());

                String shopName = xeBaseUtil.getVenShopName(responseDTO.getUserCode());
                String venName = xeBaseUtil.getVenName(responseDTO.getUserCode());
                if (StringUtils.isBlank(shopName)) {
                    responseDTO.setShopName(venName);
                } else {
                    responseDTO.setShopName(shopName);
                }
                transferList.add(responseDTO);
            }
            // 分页信息
            XePageInfoDto pageInfo = new XePageInfoDto(resultList);
            pageDto.setList(transferList);
            pageDto.setXePageInfoDto(pageInfo);
            pageDto.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.error("支付中心 > 结算管理 > 结算白名单使用接口失败：{}", e);
            pageDto.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return pageDto;
    }

    /**
     * 支付中心通知更新退款单状态为已完成
     *
     * @author 李贝贝
     */
    @RequestMapping("/finishRefundAmt")
    public BaseResponseDTO finishRefundAmt(@RequestBody XeVenRefundDTO paramDto) {
        String incomParamJson = JSONObject.toJSONString(paramDto);
        log.info("支付中心通知更新退款单状态接口传入参数： {}", incomParamJson);
        XeVenOrderRefundEntity entity = new XeVenOrderRefundEntity();
        //如果退款成功，更新退款单状态
        entity.setRefundStatus(VenOrderRefundStatusEnum.DONE.getValue());
        entity.setRefundCode(paramDto.getRefundCode());
        try {
            entity.setRefundDate(new Date());
            int result = xeVenOrderReFundService.finishRefund(entity);
            log.info("支付中心通知更新退款单{},状态执行，{}", entity.getRefundCode(), result);
        } catch (Exception e) {
            log.error("支付中心通知更新退款单状态为已完成,异常：", e);
            return sendApiError(paramDto.getSpanId());
        }
        return sendApiOK(paramDto.getSpanId());
    }

}
  
